/*
 * cgolay-alert.js v0.0.2
 * ================================================
 * Copyright 2012 Claybox Ltd
 * 
 * v0.0.2 is main to set the alert options
 * type(string): error, info, success, warning
 * buttons: show in the foot of the alert dialog
 * height: int
 * width: int
 * modal: true or false
 * show: the effect of the alert show
 **/
!function ($) {
    Alert = function (el , option) {
        var elOverlay,
        configDispear = 3;
        var isModal = option.modal ? option.modal : false;
        this.settings = {};
        this.settings.modal = isModal;
        
        el.addClass('alert').addClass(option['type']);
        if(option['modal']){
            elOverlay = $('<div id="overlay"></div>').empty().appendTo($(document.body));
            elOverlay.width($(document).width()).height($(document).height());
        }
        if(option['height'] != -1){
            $(el).css("height", option["height"])
        }
        if(option['width'] != -1){
            $(el).css("width", option["width"])
        }
        
        if(option['buttons'].length){
            $('<div class="clear"></div>').appendTo(el);
            var elBtns = $('<p class="btn-group"></p>');
            $(option['buttons']).each(function(){
                var alertBtn = $('<a class="btn"></a>').text(this.text).appendTo(elBtns);
                if(typeof this.type != "undefined"){
                    alertBtn.addClass(this.type);
                }
                if ($.isFunction(this.click)){
                    var btnCb = this.click;
                    alertBtn.click(function(){
                        btnCb.apply(el, arguments);
                    });
                }
            });
            elBtns.appendTo(el);
        }
        
        if(option['show'] == "show"){
            show();
        }else if(option['show'] == "fade"){
            fade();
        }else if(option['show'] == "slide"){
            slide();
        }else{
            show();
        }
        
        $('button.close',el).click(function(){
            close();
        });
        function show() {
            $(el).appendTo($('body'));
        }
        
        function fade() {
            $(el).fadeIn(1000).appendTo($('body'));
        }
        
        function slide() {
            $(el).slideToggle("slow").appendTo($('body'));
        }
        
        function close() {
            if(option['modal']){
                $(elOverlay).remove();
            }
            if(option['show'] == 'show'){
                el.remove();
            }else if(option['show'] == 'fade'){
                $(el).fadeOut(1000);
            }else if(option['show'] == 'slide'){
                $(el).slideToggle("slow");
            }
        }
    }
    
    $.fn.alert = function (option) {
        var $this = $(this);
        var defaultOption = {
            type: "alert-info",
            buttons: [],
            height: -1,
            width: -1,
            modal: false,
            show: "show"
        }        
        //default option 
        if(!$this.data('widget_alert')){
            var applyOption = $.extend({} , defaultOption , option);
            var v2_alert = new Alert($this , applyOption);
            $this.data('widget_alert', v2_alert);
        }else{
//            var 
            if(typeof arguments[0] === "string"){
                
            }else if(typeof arguments[0] === "object"){
                alert("o");
            }
        }
        return $this;
    }
    //        if(arguments.length == 1)return;
    $.fn.alert.Constructor = Alert;
}(window.jQuery);

